package game;

import game.Card.Suit;
import game.Card.Value;

import java.util.*;

public class Player {
	private String name = "";
	private int cash;
	private Card card1, card2;
	private int currentStake;
	private boolean foldedForThisHand;
	private boolean playedThisRound;
	public Player(){
		cash = 1000;
		foldedForThisHand = false;
		currentStake = 0;
		card1 = new Card(Suit.CLUBS,Value.ACE);
		card2 = new Card(Suit.SPADES,Value.ACE);
		playedThisRound = false;
	}
	public int getStake(){
		return currentStake;
	}
	public int withDrawCash(int amount){
		System.out.println("Before cash:"+cash);
		currentStake += amount;
		cash -= amount;
		System.out.println("Cash:"+cash);
		return amount;
	}
	public void depositWinnings(int amount){
		cash += amount;
	}
	public void setName(String s){
		name = s;
	}
	public Player(String playerRep) {
		playerRep = playerRep.replace(";", "\n");
		Scanner scan = new Scanner(playerRep);
		String line0 = scan.nextLine();
		name = line0.split("=")[1];
		String line1 = scan.nextLine();
		String cashStr = line1.split("=")[1];
		cash = Integer.parseInt(cashStr);
		String line2 = scan.nextLine();
		String foldedStr = line2.split("=")[1];
		foldedForThisHand = Boolean.parseBoolean(foldedStr);
		String line3 = scan.nextLine();
		String stakeStr = line3.split("=")[1];
		currentStake = Integer.parseInt(stakeStr);
		String line4 = scan.nextLine();
		String card1Str = line4.split("=")[1];
		card1 = new Card(card1Str);
		String line5 = scan.nextLine();
		String card2Str = line5.split("=")[1];
		card2 = new Card(card2Str);
		String line6 = scan.nextLine();
		String played = line6.split("=")[1];
		playedThisRound = Boolean.parseBoolean(played);
		scan.close();
		
	}
	
	public String toString(){
		String ret = "name="+name+";";
		ret += "cash="+cash+";";
		ret += "folded="+foldedForThisHand+";";
		ret += "stake="+currentStake+";";
		ret += "card1=" + card1.toString() + ";";
		ret += "card2=" + card2.toString() + ";";
		ret += "playedyet="+ playedThisRound+";";
		return ret;
		
	}
	@Override
	public boolean equals(Object o){
		if (o instanceof Player){
			Player p = (Player)o;
			boolean trip = false;
			if (!name.equals(p.name)){
				trip = true;
			}
			if (cash != p.cash){
				trip = true;
			}
			if (!card1.equals(p.card1)||!card2.equals(p.card2)){
				trip = true;
			}
			if (currentStake != p.currentStake){
				trip = true;
			}
			if (foldedForThisHand != p.foldedForThisHand){
				trip = true;
			}
			return !trip;
		}
		else{
			return false;
		}
	}
	public void setCard1(Card pop) {
		card1 = pop;
		
	}
	public void setCard2(Card pop) {
		card2 = pop;
		
	}
	public String getName() {
		return name;
	}
	public void foldHand() {
		foldedForThisHand = true;
		
	}
	public void setPlayed(boolean b){
		playedThisRound = b;
	}
	public boolean hasFolded() {
		return foldedForThisHand;
	}
	public boolean hasHadchanceToPlay() {
		return playedThisRound;
	}
	public void newHand() {
		foldedForThisHand = false;
		currentStake = 0;
		
	}
	public Card getCard1() {
		return card1;
	}
	public Card getCard2() {
		return card2;
	}
}
